package bg.btanev.eightyfour.dao;

import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang.Validate;

/**
 *
 * @author B.Tanev
 *
 */
public class PagingConfig {
	private final int beginRow;
	private final int maxResults;
	private final List<OrderConfig> orderList = new LinkedList<OrderConfig>();

	public PagingConfig(int beginRow, int maxResults) {
		super();

		Validate.isTrue(beginRow >= -1);
		Validate.isTrue(maxResults >= -1);

		if (beginRow != -1 && maxResults < 1) {
			throw new IllegalArgumentException();
		}

		this.beginRow = beginRow;
		this.maxResults = maxResults;
	}

	public int getBeginRow() {
		return beginRow;
	}

	public int getMaxResults() {
		return maxResults;
	}

	public List<OrderConfig> getOrderList() {
		return orderList;
	}

	public void addOrder(OrderConfig order) {
		if (!(orderList.contains(order))) {
			orderList.add(order);
		}
	}

	public static class OrderConfig {
		private final String property;
		private final boolean descending;

		public OrderConfig(String property, boolean descending) {
			super();

			Validate.notEmpty(property);

			this.property = property;
			this.descending = descending;
		}

		public String getProperty() {
			return property;
		}

		public boolean isDescending() {
			return descending;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + (descending ? 1231 : 1237);
			result = prime * result + ((property == null) ? 0 : property.hashCode());
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			OrderConfig other = (OrderConfig) obj;
			if (descending != other.descending)
				return false;
			if (property == null) {
				if (other.property != null)
					return false;
			} else if (!property.equals(other.property))
				return false;
			return true;
		}

		@Override
		public String toString() {
			return "OrderConfig [descending=" + descending + ", property=" + property + "]";
		}
	}
}
